package org.openkoala.koala.wizard.wizardpage.dialog;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.openkoala.koala.wizard.wizardpage.ModuleConfigWizardPage;

/**
 * 权限的详细的配置界面
 * @author lingen
 *
 */
public class SecurityMonitorConfigDialog extends Dialog {

	
	private ModuleConfigWizardPage moduleConfig;
	
	private String cacheType;
	
	private String template;
	
	
	private Button templateA;
	private Button templateB;
	private Button templateC;
	
	private Button memCached;
	private Button ehCache;;
	
	public SecurityMonitorConfigDialog(Shell parentShell,ModuleConfigWizardPage moduleConfig,String cacheType,String template) {
		super(parentShell);
		this.moduleConfig = moduleConfig;
		this.cacheType  = cacheType;
		this.template = template;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		chooseCacheType(parent);
		chooseAuthorityTemplate(parent);
		return super.createDialogArea(parent);
	}

	@Override
	protected void okPressed() {
		if(!template.equals(this.getTempateType()) || !this.cacheType.equals(this.getCacheType())){
			cacheType = this.getCacheType();
			template = this.getTempateType();
			moduleConfig.refreshSecurityConfig(cacheType, template);
		}
		super.okPressed();
	}


	
	/**
	 * 显示缓存类型供用户选择
	 * @param composite
	 */
	private void chooseCacheType(Composite composite) {
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.grabExcessHorizontalSpace = true;

		Group group = new Group(composite, SWT.NULL);
		group.setText("choose cache type");
		group.setLayout(new GridLayout(3, false));
		group.setLayoutData(gridData);

		ehCache = new Button(group, SWT.RADIO);
		ehCache.setLayoutData(gridData);
		ehCache.setText("ehCache");
		if(cacheType.equals("ehCache"))ehCache.setSelection(true);

		memCached = new Button(group, SWT.RADIO);
		memCached.setLayoutData(gridData);
		memCached.setText("memCached");
		if(cacheType.equals("memCached"))memCached.setSelection(true);
	}
	
	
	
	/**
	 * 返回用户选择的cacheType
	 * @return
	 */
	private String getCacheType(){
		if(ehCache.getSelection()){
			return ehCache.getText();
		}
		if(memCached.getSelection()){
			return memCached.getText();
		}
		return null;
	}
	
	private String getTempateType(){
		if(templateA.getSelection()){
			return templateA.getText();
		}
		if(templateB.getSelection()){
			return templateB.getText();
		}
		if(templateC.getSelection()){
			return templateC.getText();
		}
		
		return null;
	}
	
	
	/**
	 * 显示各个模版类型用户选择生成统一认证系统的模版
	 * @param composite
	 */
	private void chooseAuthorityTemplate(Composite composite) {
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.grabExcessHorizontalSpace = true;

		Group templatesGroup = new Group(composite, SWT.NULL);
		templatesGroup.setText("choose authority template");
		templatesGroup.setLayout(new GridLayout(3, false));
		templatesGroup.setLayoutData(gridData);

		templateA = new Button(templatesGroup, SWT.RADIO);
		templateA.setLayoutData(gridData);
		templateA.setText("TemplateA");
		if(template.equals("TemplateA"))templateA.setSelection(true);

		templateB = new Button(templatesGroup, SWT.RADIO);
		templateB.setLayoutData(gridData);
		templateB.setText("TemplateB");
		if(template.equals("TemplateB"))templateB.setSelection(true);

		templateC = new Button(templatesGroup, SWT.RADIO);
		templateC.setLayoutData(gridData);
		templateC.setText("TemplateC");
		if(template.equals("TemplateC"))templateC.setSelection(true);

		Label templateAImage = new Label(templatesGroup, SWT.NONE);
		templateAImage.setImage(ImageDescriptor.createFromURL(getClass().getResource("template1.jpg")).createImage());

		Label templateBImage = new Label(templatesGroup, SWT.NONE);
		templateBImage.setImage(ImageDescriptor.createFromURL(getClass().getResource("template2.jpg")).createImage());

		Label templateCImage = new Label(templatesGroup, SWT.NONE);
		templateCImage.setImage(ImageDescriptor.createFromURL(getClass().getResource("template3.jpg")).createImage());
	}
}
